﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using PH.Model.Analyses;
using PH.Model.Analyses.Schools;
using PH.Logic.Analyses;
namespace PH.Web.Management.Areas.Analyses.Controllers
{
    [RouteArea("Analyses")]
    [RoutePrefix("ANS")]
    public class ANSController : Controller
    {
        /// <summary>
        /// 根据路由判断是否是历史数据
        /// </summary>
        private bool History
        {
            get
            {
                var path = Request.Path;
                path = path.Replace("/Analyses/ANS/", "");
                return path.IndexOf("PS4") >= 0;
            }
        }

        [Route("RS4Class")]
        [Route("PS4Class")]
        public ActionResult S4Class()
        {
            ViewBag.History = History;
            return View();
        }

        [Route("ExportRS4Class")]
        [Route("ExportPS4Class")]
        public ActionResult ExportS4Class()
        {
            try
            {

                if (!string.IsNullOrEmpty(Request.Form.Get("aGuid")))
                {
                    if (!string.IsNullOrEmpty(Request.Form.Get("sGuid")))
                    {
                        AnalysisQuery sea = new AnalysisQuery();
                        sea.ArchiveGuid = new Guid(Request.Form.Get("aGuid"));
                        sea.SchoolGuid = new Guid(Request.Form.Get("sGuid"));
                        var s4class = SchoolLogic.GetS4Class(sea);
                        if (s4class.TotalRecordsCount > 0)
                        {
                            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                            workbook.Open(Server.MapPath("/RootFiles/Templates/Analyses/s4class.xls"));
                            var sheet = workbook.Worksheets[0];
                            var cells = sheet.Cells;

                            for (var i = 0; i < s4class.TotalRecordsCount; i++)
                            {
                                cells.CopyRow(cells, 3, i + 4);
                                cells[i + 4, 0].PutValue(s4class[i].ClassName);
                                cells[i + 4, 1].PutValue(s4class[i].Samples);
                                cells[i + 4, 2].PutValue(s4class[i].Excellent);
                                cells[i + 4, 3].PutValue(s4class[i].ExcellentRate);
                                cells[i + 4, 4].PutValue(s4class[i].Good);
                                cells[i + 4, 5].PutValue(s4class[i].GoodRate);
                                cells[i + 4, 6].PutValue(s4class[i].Pass);
                                cells[i + 4, 7].PutValue(s4class[i].PassRate);
                                cells[i + 4, 8].PutValue(s4class[i].Fail);
                                cells[i + 4, 9].PutValue(s4class[i].FailRate);
                                cells[i + 4, 10].PutValue(s4class[i].Qualified);
                                cells[i + 4, 11].PutValue(s4class[i].QualifiedRate);
                                cells[i + 4, 12].PutValue(s4class[i].Average);
                            }
                            cells.DeleteRow(3);
                            var buffer = workbook.SaveToStream().ToArray();
                            return File(buffer, "application/octet-stream", "班级分析表.xls");
                        }
                        else
                            return Content("暂无数据");
                    }
                    else
                        return Content("不存在的学校编号");
                }
                else
                    return Content("不存在的档案编号");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }

        [Route("RS4Sex")]
        [Route("PS4Sex")]
        public ActionResult S4Sex()
        {
            ViewBag.History = History;
            return View();
        }

        [Route("ExportRS4Sex")]
        [Route("ExportPS4Sex")]
        public ActionResult ExportS4Sex()
        {
            try
            {

                if (!string.IsNullOrEmpty(Request.Form.Get("aGuid")))
                {
                    if (!string.IsNullOrEmpty(Request.Form.Get("sGuid")))
                    {
                        AnalysisQuery sea = new AnalysisQuery();
                        sea.ArchiveGuid = new Guid(Request.Form.Get("aGuid"));
                        sea.SchoolGuid = new Guid(Request.Form.Get("sGuid"));
                        sea.History = History;
                        var s4sex = SchoolLogic.GetS4Sex(sea);
                        if (s4sex.TotalRecordsCount > 0)
                        {
                            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                            workbook.Open(Server.MapPath("/RootFiles/Templates/Analyses/s4sex.xls"));
                            var sheet = workbook.Worksheets[0];
                            var cells = sheet.Cells;

                            for (var i = 0; i < s4sex.TotalRecordsCount; i++)
                            {
                                cells[i + 3, 0].PutValue(s4sex[i].Sex == 1 ? "男" : "女");
                                cells[i + 3, 1].PutValue(s4sex[i].Samples);
                                cells[i + 3, 2].PutValue(s4sex[i].Excellent);
                                cells[i + 3, 3].PutValue(s4sex[i].ExcellentRate);
                                cells[i + 3, 4].PutValue(s4sex[i].Good);
                                cells[i + 3, 5].PutValue(s4sex[i].GoodRate);
                                cells[i + 3, 6].PutValue(s4sex[i].Pass);
                                cells[i + 3, 7].PutValue(s4sex[i].PassRate);
                                cells[i + 3, 8].PutValue(s4sex[i].Fail);
                                cells[i + 3, 9].PutValue(s4sex[i].FailRate);
                                cells[i + 3, 10].PutValue(s4sex[i].Qualified);
                                cells[i + 3, 11].PutValue(s4sex[i].QualifiedRate);
                                cells[i + 3, 12].PutValue(s4sex[i].Average);
                            }
                            var buffer = workbook.SaveToStream().ToArray();
                            return File(buffer, "application/octet-stream", "性别分析表.xls");
                        }
                        else
                            return Content("暂无数据");
                    }
                    else
                        return Content("不存在的学校编号");
                }
                else
                    return Content("不存在的档案编号");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }

        [Route("RS4Grade")]
        [Route("PS4Grade")]
        public ActionResult S4Grade()
        {
            ViewBag.History = History;
            return View();
        }

        [Route("ExportRS4Grade")]
        [Route("ExportPS4Grade")]
        public ActionResult ExportS4Grade()
        {
            try
            {

                if (!string.IsNullOrEmpty(Request.Form.Get("aGuid")))
                {
                    if (!string.IsNullOrEmpty(Request.Form.Get("sGuid")))
                    {
                        AnalysisQuery sea = new AnalysisQuery();
                        sea.ArchiveGuid = new Guid(Request.Form.Get("aGuid"));
                        sea.SchoolGuid = new Guid(Request.Form.Get("sGuid"));
                        sea.History = History;
                        var s4teacher = SchoolLogic.GetS4Grade(sea);
                        if (s4teacher.TotalRecordsCount > 0)
                        {
                            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                            workbook.Open(Server.MapPath("/RootFiles/Templates/Analyses/s4grade.xls"));
                            var sheet = workbook.Worksheets[0];
                            var cells = sheet.Cells;

                            for (var i = 0; i < s4teacher.TotalRecordsCount; i++)
                            {
                                cells.CopyRow(cells, 3, i + 4);
                                cells[i + 4, 0].PutValue(s4teacher[i].GradeName);
                                cells[i + 4, 1].PutValue(s4teacher[i].Samples);
                                cells[i + 4, 2].PutValue(s4teacher[i].Excellent);
                                cells[i + 4, 3].PutValue(s4teacher[i].ExcellentRate);
                                cells[i + 4, 4].PutValue(s4teacher[i].Good);
                                cells[i + 4, 5].PutValue(s4teacher[i].GoodRate);
                                cells[i + 4, 6].PutValue(s4teacher[i].Pass);
                                cells[i + 4, 7].PutValue(s4teacher[i].PassRate);
                                cells[i + 4, 8].PutValue(s4teacher[i].Fail);
                                cells[i + 4, 9].PutValue(s4teacher[i].FailRate);
                                cells[i + 4, 10].PutValue(s4teacher[i].Qualified);
                                cells[i + 4, 11].PutValue(s4teacher[i].QualifiedRate);
                                cells[i + 4, 12].PutValue(s4teacher[i].Average);
                            }
                            cells.DeleteRow(3);
                            var buffer = workbook.SaveToStream().ToArray();
                            return File(buffer, "application/octet-stream", "年级分析表.xls");
                        }
                        else
                            return Content("暂无数据");
                    }
                    else
                        return Content("不存在的学校编号");
                }
                else
                    return Content("不存在的档案编号");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }

        [Route("RS4Teacher")]
        [Route("PS4Teacher")]
        public ActionResult S4Teacher()
        {
            ViewBag.History = History;
            return View();
        }

        [Route("ExportRS4Teacher")]
        [Route("ExportPS4Teacher")]
        public ActionResult ExportS4Teacher()
        {
            try
            {

                if (!string.IsNullOrEmpty(Request.Form.Get("aGuid")))
                {
                    if (!string.IsNullOrEmpty(Request.Form.Get("sGuid")))
                    {
                        AnalysisQuery sea = new AnalysisQuery();
                        sea.ArchiveGuid = new Guid(Request.Form.Get("aGuid"));
                        sea.SchoolGuid = new Guid(Request.Form.Get("sGuid"));
                        sea.History = History;
                        var s4teacher = SchoolLogic.GetS4Teacher(sea);
                        if (s4teacher.TotalRecordsCount > 0)
                        {
                            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                            workbook.Open(Server.MapPath("/RootFiles/Templates/Analyses/s4teacher.xls"));
                            var sheet = workbook.Worksheets[0];
                            var cells = sheet.Cells;

                            for (var i = 0; i < s4teacher.TotalRecordsCount; i++)
                            {
                                cells.CopyRow(cells, 3, i + 4);
                                cells[i + 4, 0].PutValue(s4teacher[i].TeacherName);
                                cells[i + 4, 1].PutValue(s4teacher[i].Samples);
                                cells[i + 4, 2].PutValue(s4teacher[i].Excellent);
                                cells[i + 4, 3].PutValue(s4teacher[i].ExcellentRate);
                                cells[i + 4, 4].PutValue(s4teacher[i].Good);
                                cells[i + 4, 5].PutValue(s4teacher[i].GoodRate);
                                cells[i + 4, 6].PutValue(s4teacher[i].Pass);
                                cells[i + 4, 7].PutValue(s4teacher[i].PassRate);
                                cells[i + 4, 8].PutValue(s4teacher[i].Fail);
                                cells[i + 4, 9].PutValue(s4teacher[i].FailRate);
                                cells[i + 4, 10].PutValue(s4teacher[i].Qualified);
                                cells[i + 4, 11].PutValue(s4teacher[i].QualifiedRate);
                                cells[i + 4, 12].PutValue(s4teacher[i].Average);
                            }
                            cells.DeleteRow(3);
                            var buffer = workbook.SaveToStream().ToArray();
                            return File(buffer, "application/octet-stream", "教师分析表.xls");
                        }
                        else
                            return Content("暂无数据");
                    }
                    else
                        return Content("不存在的学校编号");
                }
                else
                    return Content("不存在的档案编号");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }

        [Route("RS4Sample")]
        [Route("PS4Sample")]
        public ActionResult S4Sample()
        {
            ViewBag.History = History;
            return View();
        }

        [Route("RS4Subject")]
        [Route("PS4Subject")]
        public ActionResult S4Subject()
        {
            ViewBag.History = History;
            return View();
        }

        [Route("ExportRS4Subject")]
        [Route("ExportPS4Subject")]
        public ActionResult ExportS4Subject()
        {
            try
            {

                if (!string.IsNullOrEmpty(Request.Form.Get("aGuid")))
                {
                    if (!string.IsNullOrEmpty(Request.Form.Get("sGuid")))
                    {
                        AnalysisQuery sea = new AnalysisQuery();
                        sea.ArchiveGuid = new Guid(Request.Form.Get("aGuid"));
                        sea.SchoolGuid = new Guid(Request.Form.Get("sGuid"));
                        sea.History = History;
                        var s4subject = SchoolLogic.GetS4Subject(sea);
                        if (s4subject.TotalRecordsCount > 0)
                        {
                            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                            workbook.Open(Server.MapPath("/RootFiles/Templates/Analyses/s4grade.xls"));
                            var sheet = workbook.Worksheets[0];
                            var cells = sheet.Cells;

                            for (var i = 0; i < s4subject.TotalRecordsCount; i++)
                            {
                                cells.CopyRow(cells, 3, i + 4);
                                cells[i + 4, 0].PutValue(s4subject[i].SubjectName);
                                cells[i + 4, 1].PutValue(s4subject[i].Samples);
                                cells[i + 4, 2].PutValue(s4subject[i].Excellent);
                                cells[i + 4, 3].PutValue(s4subject[i].ExcellentRate);
                                cells[i + 4, 4].PutValue(s4subject[i].Good);
                                cells[i + 4, 5].PutValue(s4subject[i].GoodRate);
                                cells[i + 4, 6].PutValue(s4subject[i].Pass);
                                cells[i + 4, 7].PutValue(s4subject[i].PassRate);
                                cells[i + 4, 8].PutValue(s4subject[i].Fail);
                                cells[i + 4, 9].PutValue(s4subject[i].FailRate);
                                cells[i + 4, 10].PutValue(s4subject[i].Qualified);
                                cells[i + 4, 11].PutValue(s4subject[i].QualifiedRate);
                                cells[i + 4, 12].PutValue(s4subject[i].Average);
                            }
                            cells.DeleteRow(3);
                            var buffer = workbook.SaveToStream().ToArray();
                            return File(buffer, "application/octet-stream", "项目分析表.xls");
                        }
                        else
                            return Content("暂无数据");
                    }
                    else
                        return Content("不存在的学校编号");
                }
                else
                    return Content("不存在的档案编号");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }
    }
}
